﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MaxLeaf.DB;

using MaxLeaf.Common;

namespace MaxLeaf.DAL.K2W
{
    /// <summary>
    /// 分发基类,add/update/delete
    /// </summary>
    public class BaseDAL : IDALAction
    {
        public enum GetDataType
        {
            OrgData

        }

        protected string strTableName = null;
        protected string strOrgTableName = null;
        
        protected string strKeyFieldName = null;
        protected string strOrgKeyFieldName = null;
        //除key以外需要维护的字段
        protected string strFieldNames = null;
        protected string strOrgFieldNames = null;

        public virtual void Delete(DBAccess dbOrgK3Con, DBAccess dbDestWebReadCon, DBAccess dbDestWebWriteCon, string strKeyValue)
        {
            KeyValuePair<string, IDataParameter[]> kvp = dbDestWebWriteCon.CreateCondParam(new Dictionary<string, object> { { strKeyFieldName, strKeyValue } });
            string strSql = string.Format(@"update {0} set is_delete=1 where  {1}", strTableName, kvp.Key);

            dbDestWebWriteCon.ExecuteSql(strSql, kvp.Value);



        }

        public virtual int Add(DBAccess dbOrgK3Con, DBAccess dbDestWebReadCon, DBAccess dbDestWebWriteCon, string strKeyValue)
        {

            return Update(dbOrgK3Con, dbDestWebReadCon,dbDestWebWriteCon, strKeyValue);

        }

        /// <summary>
        /// 每次全部update
        /// </summary>
        /// <param name="dbOrgK3Con">来源数据库</param>
        /// <param name="dbDestWebReadCon">目标数据库</param>
        /// <param name="strKeyValue"></param>
        public virtual int Update(DBAccess dbOrgK3Con, DBAccess dbDestWebReadCon, DBAccess dbDestWebWriteCon, string strKeyValue)
        {
            string strUpdateFieldNames = strFieldNames;
            string strOrgUpdateFieldNames = strOrgFieldNames;

            DataTable dtData = dbDestWebReadCon.GetTableData(strTableName, new Dictionary<string, object> { { strKeyFieldName, strKeyValue } }, strKeyFieldName + "," + strUpdateFieldNames);
            DataRow drData = null;
            LogStatus logStatus = LogStatus.Update;
            if (dtData.Rows.Count == 0)
            {
                logStatus = LogStatus.Add;
                drData = dtData.NewRow();
                drData[strKeyFieldName] = strKeyValue;

                strUpdateFieldNames = strKeyFieldName + "," + strUpdateFieldNames;
                strOrgUpdateFieldNames = strOrgKeyFieldName + "," + strOrgUpdateFieldNames;
            }
            else
            {
                drData = dtData.Rows[0];
            }



            DataTable dtOrgData = DAL_GetData(dbOrgK3Con, dbDestWebReadCon,dbDestWebWriteCon, (int)GetDataType.OrgData, strKeyValue);
            if (dtOrgData == null || dtOrgData.Rows.Count == 0)
            {
                return 0;
            }
            DataRow drK3OldData = dtOrgData.Rows[0];
            string[] arrFieldName = strUpdateFieldNames.Split(',');
            string[] arrOrgFieldName = strOrgUpdateFieldNames.Split(',');
            for (int i=0 ;i< arrFieldName.Length;i++)
            {
                string strField =arrFieldName[i];
                string strOrgField = strField;
                if (arrOrgFieldName.Length > i && arrOrgFieldName.Length == arrFieldName.Length)
                {
                    strOrgField = arrOrgFieldName[i];
                }
                DAL_GetFieldData(dbOrgK3Con, dbDestWebReadCon,dbDestWebWriteCon, strField, strOrgField, drK3OldData, drData);
            }

            KeyValuePair<string, IDataParameter[]> kvp = dbDestWebWriteCon.CreateParamSql(strUpdateFieldNames, strTableName, drData, logStatus, strKeyFieldName);

            dbDestWebWriteCon.ExecuteSql(kvp.Key, kvp.Value);

            return CvtFunc.ToInt(drData[strKeyFieldName]);
        }
        /// <summary>
        /// 根据FielName赋值
        /// </summary>
        /// <param name="dbOrgK3Con"></param>
        /// <param name="dbDestWebReadCon"></param>
        /// <param name="strFieldName"></param>
        /// <param name="drOrgData"></param>
        /// <param name="drDestData"></param>
        protected virtual void DAL_GetFieldData(DBAccess dbOrgK3Con, DBAccess dbDestWebReadCon, DBAccess dbDestWebWriteCon, string strFieldName, string strOrgFieldName, DataRow drOrgData, DataRow drDestData)
        {
            drDestData[strFieldName] = drOrgData[strOrgFieldName];
        }

        protected virtual DataTable DAL_GetData(DBAccess dbOrgK3Con, DBAccess dbDestWebReadCon, DBAccess dbDestWebWriteCon, int dataType, object objParam)
        {

            if (dataType == (int)GetDataType.OrgData)
            {
                return dbOrgK3Con.GetTableData(strOrgTableName, new Dictionary<string, object> { { strOrgKeyFieldName , CvtFunc.ToString(objParam) } });
                    
                 
            }


            throw new MaxLeafException(string.Format("GetData {0} 没注册", dataType));
        }

    }
}
